package logic

import (
	"context"
	"errors"

	"test-api/internal/model"
	"test-api/internal/svc"
	"test-api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
	"gorm.io/gorm"
)

type UserInfoLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUserInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserInfoLogic {
	return &UserInfoLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UserInfoLogic) UserInfo(req *types.STUserInfoReq) (resp *types.STUserInfoRsp, err error) {
	resp = &types.STUserInfoRsp{}

	v := &model.User{}

	db := l.svcCtx.DB.Model(&model.User{}).Where("id = ?", req.Id).Where("state = 1")

	if err = db.First(&v).Error; err != nil {
		if err == gorm.ErrRecordNotFound {
			return resp, nil
		}
		return resp, errors.New("数据库错误")
	}

	return &types.STUserInfoRsp{
		Name:   v.Name,
		Mobile: v.Mobile,
	}, nil
}
